/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
*/
package uk.co.gidley.teamAlert.listenerServices;

import org.apache.commons.lang.StringUtils;
import org.jivesoftware.smack.packet.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.gidley.teamAlert.vo.Alert;
import uk.co.gidley.teamAlert.vo.AlertType;

/**
 * Created by IntelliJ IDEA. User: ben Date: Jun 17, 2008 Time: 7:06:59 AM
 */
public class TextBasedMessageDecoder implements MessageDecoder {
	private final Logger logger = LoggerFactory.getLogger(TextBasedMessageDecoder.class);


	private String failedPattern;
	private String passedPattern;
	private static final String CHANGES_MADE_BY = "Change made by ";
	private static final String HTTP = "http";


	public Alert decodeMessage(Message message) throws UnableToDecodeException {


		Alert alert = new Alert();
		logger.debug("Message passed for decoding:{}", message);

		String body = message.getBody();
		logger.debug("Message body:{}", body);

		alert.setBuildMessage(body);
		StringBuffer shortMessage = new StringBuffer(100);
		shortMessage.append("Build ");

		if (body.contains(failedPattern)) {
			alert.setType(AlertType.BROKEN);
			shortMessage.append("broken by");
		} else if (body.contains(passedPattern)) {
			alert.setType(AlertType.FIXED);
			shortMessage.append("fixed by");
		} else {
			throw new UnableToDecodeException("Unable to work out message type:" + message.getBody());
		}

		// Find the HTTP bit
		int httpOffset = body.indexOf(HTTP);
		int changesMadeByOffset = body.indexOf(CHANGES_MADE_BY) + CHANGES_MADE_BY.length();
		if (httpOffset > 0 && changesMadeByOffset > CHANGES_MADE_BY.length()) {
			shortMessage.append(body.substring(changesMadeByOffset, httpOffset));
			alert.setShortMessage(shortMessage.toString());
		}

		if (StringUtils.isEmpty(alert.getShortMessage())) {
			// If we haven't worked out a short message use the long one
			alert.setShortMessage(body);
		}

		logger.debug("Alert is type:{} buildMessage:{} shortMessage:{}",
				new Object[] { alert.getType().toString(), alert.getBuildMessage(), alert.getShortMessage() });

		return alert;
	}

	public String getFailedPattern() {
		return failedPattern;
	}

	public void setFailedPattern(String failedPattern) {
		this.failedPattern = failedPattern;
	}

	public String getPassedPattern() {
		return passedPattern;
	}

	public void setPassedPattern(String passedPattern) {
		this.passedPattern = passedPattern;
	}
}
